package qlttn.common.utility;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * This class has method getConnect to get Connection.
 * @author Win7
 */
public class ConnectionDBUtility {
//	private static final Logger log = Logger
//			.getLogger(ConnetionDBUtility.class);

	/**
	 * Get and return Connection.
	 * @return Connection
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static Connection getConnect() throws ClassNotFoundException,
			SQLException {
//		log.info("ConnetionDBUtility.getConnect()..................start");
		Connection con = null;
		// Get data from properties file.
		ResourceBundle rb = ResourceBundle.getBundle("properties");
		// Load driver.
		Class.forName(rb.getString("Driver"));
		con = DriverManager.getConnection(rb.getString("Url"),
				rb.getString("Username"), rb.getString("Password"));
//		log.info("ConnetionDBUtility.getConnect()..................end");
		// Return a Connection.
		return con;
	}

	/**
	 * Close Connection, PreparedStatement, ResultSet
	 * @param con
	 * @param pre
	 * @param rs
	 */
	public static void closeConnection(Connection con, PreparedStatement pre,
			ResultSet rs) {
//		log.info("ConnetionDBUtility.closeConnection()..................start");
		try {
			if (con != null) {
				con.close();
			}
		} catch (SQLException r) {
		}
		try {
			if (pre != null) {
				pre.close();
			}
		} catch (SQLException p) {
		}
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException c) {
		}
//		log.info("ConnetionDBUtility.closeConnection()..................end");
	}
}
